package lijiale.project.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(urlPatterns={"/web/page/*","/market","/employee"})
public class UserFilter implements Filter{

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest req_ =(HttpServletRequest)req;//类型强制转换
		
		String action=req.getParameter("action");//如果是登录或者注册，则放行
		if("reg".equals(action)||"login".equals(action)) {
			chain.doFilter(req, resp);
			return;
		}
		
		Object user = req_.getSession().getAttribute("user");
		if(user==null) {//如果用户信息为空，跳转至登陆界面
			req.getRequestDispatcher("/web/login.jsp").forward(req, resp);
		}else   //否则放行
			chain.doFilter(req, resp);
	}

}
